//
//  jxsViewTaskDetailsViewController.m
//  ToDoList
//
// This code file is apart of project "iPhone-ToDo-List" 
// (http://code.google.com/p/iphone-todo-list/)
//
// Copyright (c) 2009 Frank Villasenor
//
// Distributed under the terms of the Lesser GNU General Public License.
// A copy of the license should be with these files. If not, you 
// can retrieve a copy from http://www.gnu.org/licenses/lgpl-3.0.txt
// 

#import "jxsViewTaskDetailsViewController.h"

#import "tosuTask.h"
#import "NSDate(DateFormatting).h"
#import "NSString(TrimExtensions).h"

#import "jxsEditTaskDetailViewController.h"
#import "ToDoListAppDelegate.h"

@implementation jxsViewTaskDetailsViewController

@synthesize taskToRepresent;
@synthesize titleLabel, dueDateLabel, isCompleteLabel, notesLabel;
@synthesize editTaskDetailsViewController;

- (void)dealloc 
{
	[taskToRepresent release];
	
	[titleLabel release];
	[dueDateLabel release];
	[isCompleteLabel release];
	[notesLabel release];
	
	[editTaskDetailsViewController release];
	
    [super dealloc];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Edit" 
		style:UIBarButtonItemStyleBordered 
		target:self 
		action:@selector(editTaskInView)];
	
	self.navigationItem.rightBarButtonItem = button;
	[button release];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	[self displayTaskInView];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark -
#pragma mark Custom Instance Methods
- (IBAction)editTaskInView
{
	if( self.editTaskDetailsViewController == nil )
	{
		jxsEditTaskDetailViewController *EditTaskViewController = [[jxsEditTaskDetailViewController alloc] initWithNibName:@"jxsEditTaskDetailsView" bundle:nil];
		self.editTaskDetailsViewController = EditTaskViewController;
		[EditTaskViewController release];
	}
	
	//
	// We need a copy in case we abort the edits.
	tosuTask *LocalTaskToEdit = [self.taskToRepresent copy];
	self.editTaskDetailsViewController.taskToEdit = LocalTaskToEdit;
	[LocalTaskToEdit release];
	
	ToDoListAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.mainNavController pushViewController:self.editTaskDetailsViewController animated:YES];
}

- (void)displayTaskInView
{
	if( self.taskToRepresent == nil || [self.taskToRepresent retainCount] == 0 )
	{
		NSLog( @"Task To Represent is nil, which means that this view can not load. Process stopped via exception." );
		[NSException raise:@"nilTaskToRepresent" format:@"The TaskToRepresent is nil; this must not be!"];
	}
	else
	{
		self.titleLabel.text = self.taskToRepresent.title;
		self.dueDateLabel.text = [self.taskToRepresent.dueDate getShortDateWithoutTime];
		self.isCompleteLabel.text = (self.taskToRepresent.isComplete == YES) ? @"Yes" : @"No";
		self.notesLabel.text = self.taskToRepresent.notes;
		
		//
		// Set the nav controller's title to that of the task. 
		// This should give the NavigationBar that [task's] title.
		self.title = self.taskToRepresent.title;	
	}
}

#pragma mark -
#pragma mark jxsTaskReceiver Protocal Methods
- (void)UpdateLocalTaskWith:(tosuTask *)newTask
{
	//
	//http://stackoverflow.com/questions/818164/objective-c-2-0-assigning-a-property-leaking-memory
	self.taskToRepresent = newTask;
	
	//
	// Referesh the control contents
	[self displayTaskInView];
	
	//
	// Pass the object down to the Root controller to save it 
	// in the main task list.
	ToDoListAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	id rootViewController = [[delegate.mainNavController viewControllers] objectAtIndex:0];
	if( [rootViewController conformsToProtocol:@protocol(jxsTaskReceiver)] )
	{
		[rootViewController UpdateLocalTaskWith:newTask];
	}
	else
	{
		[NSException raise:@"Must conform to jxsTaskReceiver" format:@"jxsTaskReceiver must be implemented"];
	}
}
- (void)addTaskWith:(tosuTask *)newTask
{
	[NSException raise:@"NotImplemented" format:@"Method is not implemented."];
}
@end
